package com.apestech.framework.dictionary.lib;

import java.util.ArrayList;

import com.apestech.formwork.stream.ListStream;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.file.callback.ReadBufferFileCallBackHandler;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DBUtil;
import com.apestech.framework.util.StringUtil;

public class ReadDictionary extends StringUtil {

	private String fileName;
	private ListStream file;
	private String type;

	public ReadDictionary(String fileName, String type) throws Exception{
		this.fileName=fileName;
		this.file= DBUtil.getDictionaryDate(fileName, type);
	}

	public Table getTable(String tabName) throws Exception{
		final Table tab=new Table(tabName);
		final String table=tabName;
		file.readKey(tabName, new ReadBufferFileCallBackHandler(){

			int inx=0;
			String col="";
			String remarks="";
			String dateCol="";
			String numCol="";
			String intCol="";

			public boolean read(String text, int index) throws Exception {
				String key="PRIMARY:";
				if(key.equals(StringUtil.leftStr(text, key.length()))){
					tab.setPkKeyfromJSON(JSON.toJSON(StringUtil.rightStr(text, (text.length()-key.length()))));
				} else {
					key="IMPORTED:";
					if(key.equals(StringUtil.leftStr(text, key.length()))){
						tab.setPkFKeyFromJSON(table, JSON.toJSON(StringUtil.rightStr(text, (text.length()-key.length()))));
					} else {
						writeCol(tab.addField(JSON.toJSON(text)));
					}
				}
				return false;
			}

			private void writeCol(String COLUMN_NAME) throws Exception {
				if((","+col+",").indexOf(","+COLUMN_NAME+",")>=0) return;
				String REMARKS=tab.getRemarks(COLUMN_NAME);
				REMARKS=formatIdMemo(REMARKS);
				if("".equals(REMARKS) || REMARKS==null){
					REMARKS=COLUMN_NAME;
				}
				if(REMARKS==null){
					REMARKS="";
				}
				if(REMARKS.indexOf("(")>=0 && REMARKS.indexOf(":")>=0 && REMARKS.indexOf(")")>=0){  //存在数据列表情况
					String memo=getSplitList(REMARKS);
					REMARKS=getSpiltId(REMARKS);
					ArrayList<ArrayList<String>> list=getStrSplitList("; :", memo);
					int len=list.size();
					for(int i=0; i<len; i++){
						String sid=list.get(i).get(0);
						try{
							String stext=list.get(i).get(1);
							tab.addList(COLUMN_NAME, sid, stext);
						} catch (Exception e){
							throw new Exception("请检查字段["+COLUMN_NAME+"]备注里的配置\r\n["+memo+"]中\r\n["+sid+"]部分是否正确！");
						}

					}
				}
				tab.setRemarks(COLUMN_NAME, REMARKS);
				if(!"".equals(col)){
					col=col+",";
				}
				col=col+COLUMN_NAME;
				if(!"".equals(remarks)){
					remarks=remarks+",";
				}
				remarks=remarks+REMARKS;

				String typeName=tab.getTypeName(COLUMN_NAME);
				if(typeName.indexOf("DATE")>=0 || typeName.indexOf("TIME")>=0){
					if(!"".equals(dateCol)){
						dateCol=dateCol+",";
					}
					dateCol=dateCol+COLUMN_NAME;
				} else if(typeName.indexOf("NUMBER")>=0){
					if(tab.getSize(COLUMN_NAME)>0){
						if(!"".equals(numCol)){
							numCol=numCol+",";
						}
						numCol=numCol+COLUMN_NAME;
					} else {
						if(!"".equals(intCol)){
							intCol=intCol+",";
						}
						intCol=intCol+COLUMN_NAME;
					}
				}

				inx++;
				tab.setColumn(col);
				tab.setDateColumn(dateCol);
				tab.setNumColumn(numCol);
				tab.setIntColumn(intCol);
				tab.setRemarks(remarks);
			}
		});
		if(tab.size()==0) return null;
		return tab;
	}

	public JSONArray getProcedure(String proName) throws Exception{
		final JSONArray proc=new JSONArray();
		file.read(proName, new ReadBufferFileCallBackHandler(){
			public boolean read(String text, int index) throws Exception {
				JSONObject map=null;
				map=JSON.toJSON(text);
				String COLUMN_NAME=map.getString("COLUMN_NAME");
				map.remove("PROCEDURE_NAME");
				proc.add(map);
				return false;
			}
		});
		if(proc.size()==0) return null;
		return proc;
	}

	public JSONObject getProcedureJson(String proName) throws Exception {
		final JSONObject proc=new JSONObject();
		file.read(proName, new ReadBufferFileCallBackHandler(){
			public boolean read(String text, int index) throws Exception {
				JSONObject map=null;
				map=JSON.toJSON(text);
				String COLUMN_NAME=map.getString("COLUMN_NAME");
				map.remove("PROCEDURE_NAME");
				map.remove("COLUMN_NAME");
				proc.put(COLUMN_NAME, map);
				return false;
			}
		});
		if(proc.size()==0) return null;
		return proc;
	}

	public void close() throws Exception {
		if(file!=null){
			file.close();
		}
	}

}
